from web.Script import haijiaoshequ
from django.http import JsonResponse

script = haijiaoshequ.ScriptHaiJiao()


# 获取热门话题

def get_hot_list(request):
    if request.method == 'GET':
        page = request.GET.get('page', 1)
        data = script.get_hot_list(page)
        return JsonResponse({'code': 200, 'msg': '请求成功', 'data': data}, status = 200, safe = None)
    return JsonResponse({'code': 400, 'msg': '请求方式错误'}, status = 405, safe = None)


# 获取话题内容
def get_topic_content(request):
    if request.method == 'GET':
        pid = request.GET.get('pid')
        data = script.get_topic_content(pid)
        return JsonResponse({"data": data}, status = 200, safe = None)
    return JsonResponse({'code': 400, 'msg': '请求方式错误'}, status = 405, safe = None)


# 搜索话题
def search_topic(request):
    if request.method == 'GET':
        keyword = request.GET.get('keyword')
        page = request.GET.get('page', 1)
        data = script.search_topic(keyword, page)
        return JsonResponse({'code': 200, 'msg': '请求成功', 'data': data}, status = 200, safe = None)
    return JsonResponse({'code': 400, 'msg': '请求方式错误'}, status = 405, safe = None)
